import { Switch, Route, Redirect } from 'react-router-dom'
import React from 'react'
export default function RouterView ({ routeList }) {
  const list = routeList && routeList.filter((item) => !item.to)
  const redirect = routeList && routeList.filter((item) => item.to)[0]

  return (
    <Switch>
      {list.map((item, index) => {
        return (
          <Route
            path={item.path}
            key={index}
            render={(routerProps) => {
              console.log(routerProps)
              const Com = item.component
              if (item.children) {
                return (
                  <Com
                    {...routerProps}
                    child={item.children}
                    navLink={item.children.filter((item) => !item.to)}
                  />
                )
              }

              return <Com {...routerProps} />
            }}
          />
        )
      })}

      {redirect ? <Redirect to={redirect.to} from={redirect.from} /> : null}
    </Switch>
  )
}
